package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.abigdreamer.im.server.module.msgProcessConfig.MessageProcessConfig;
import com.abigdreamer.im.server.serverConfig.EIMServerConfig;
import com.abigdreamer.im.server.tools.ServerUtils;
import com.abigdreamer.im.server.ui.desktop.DeskTopConfig;
import com.thoughtworks.xstream.XStream;

public class ConfigEdit extends DeskTopFrame{
	
	EditPanel editPanel=null;
	Object config=null;
	
	public ConfigEdit(){
		initFrame();
	}
	
	public ConfigEdit(String text){
		initFrame();
		this.setConfigText(text);
	}

	public ConfigEdit(String title, boolean resizable) {
		super(title, resizable);
		initFrame();
	
	}
	
	public ConfigEdit(String title, String text, boolean resizable) {
		super(title, resizable);
		initFrame();
		this.setConfigText(text);
	}
	
	private void initFrame(){
	    editPanel=new EditPanel(this);
		this.getContentPane().add(editPanel,BorderLayout.CENTER);
	}
	
    public void setConfigText(String txt){
    	editPanel.setConfigText(txt);
    }

	public EditPanel getEditPanel() {
		return editPanel;
	}

	public void setEditPanel(EditPanel editPanel) {
		this.editPanel = editPanel;
	}

	public Object getConfig() {
		return config;
	}

	public void setConfig(Object config) {
		this.editPanel.setConfig(config);
		this.config = config;
	}
}

class EditPanel extends javax.swing.JPanel {
	ConfigEdit configEdit=null;
    public EditPanel(ConfigEdit configEdit) {
        initComponents();
        this.configEdit=configEdit;
    }
    
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        configText = new javax.swing.JTextPane();
        save = new javax.swing.JButton();
        use = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        configText.setBackground(new java.awt.Color(0, 0, 0));
        configText.setFont(new java.awt.Font("宋体", 0, 14));
        configText.setForeground(new java.awt.Color(0, 204, 0));
        jScrollPane1.setViewportView(configText);

        save.setText("保存");

        use.setText("应用");

        exit.setText("取消");
        
        save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editAction(e);
			}
		});
        use.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editAction(e);
			}
		});
        exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editAction(e);
			}
		});
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(save)
                .addGap(28, 28, 28)
                .addComponent(use)
                .addGap(41, 41, 41)
                .addComponent(exit)
                .addContainerGap(150, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(save)
                    .addComponent(use)
                    .addComponent(exit))
                .addContainerGap())
        );
    }                       
    
    protected void editAction(ActionEvent e) {
    	String str = e.getActionCommand();
    	if(save.getText().equals(str)){
    		int sure=getOptionDialog("保存配置");
        	if(sure==0){
        		write();
        	}
			
    	}else if(use.getText().equals(str)){
    		int sure=getOptionDialog("应用配置");
        	if(sure==0){
        		write();
        	}
    		
    	}else if(exit.getText().equals(str)){
    		this.configEdit.setVisible(false);
    	}
	}
    /**
     * 
     */
    private void write(){
    	System.out.println(configText.getText());
    	if(config instanceof EIMServerConfig ){
			XStream xstream=ServerUtils.getProcessServserConfigXStream();
			EIMServerConfig serverConfig=(EIMServerConfig)xstream.fromXML(configText.getText());
			ServerUtils.writeServerConfig(serverConfig);
		}else if(config instanceof DeskTopConfig){
			XStream xstream=ServerUtils.getDeskTopConfigXStream();
			DeskTopConfig deskTopConfig=(DeskTopConfig)xstream.fromXML(configText.getText());
			ServerUtils.writeDeskTopConfig(deskTopConfig);
		}
		else if(config instanceof MessageProcessConfig){
			XStream xstream	=ServerUtils.getProcessConfigXStream();
			MessageProcessConfig msgProcessConfig=(MessageProcessConfig)xstream.fromXML(configText.getText());
			ServerUtils.writeProcessConfig(msgProcessConfig);
		}
    }
    /**
     * 
     * @param msg
     * @return
     */
    private int getOptionDialog(String msg){
    	Object[] options = {"确定?","取消?"};
    	int response=JOptionPane.showOptionDialog ( this, "您确定"+msg,msg,JOptionPane.YES_OPTION ,JOptionPane.PLAIN_MESSAGE,
                null, options, options[0] );
    	return response;
    }
    
	public void setConfigText(String txt){
    	configText.setText(txt);
    }
    
	public Object getConfig() {
		return config;
	}

	public void setConfig(Object config) {
		this.config = config;
	}
	
    // 变量声明 - 不进行修改                 
	Object config=null;
    private javax.swing.JTextPane configText;
    private javax.swing.JButton exit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton save;
    private javax.swing.JButton use;

}